package com.example.xuegengwang.xuegengwang.service;

import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.HashMap;

import common.Constant;
import common.Variable;
import https.InterNetController;
import utils.MyLogUtils;
import utils.SPUtils;
import utils.VariousUtils;

/**
 * Created by Administrator on 2017/6/16.
 */

public class UpdateService extends Service {
    private MyHandler myHandler1;

    private static class MyHandler extends Handler {
        WeakReference<UpdateService> weakReference;

        public MyHandler(UpdateService weakReference) {
            this.weakReference = new WeakReference<>(weakReference);
        }

        @Override
        public void handleMessage(Message msg) {
            UpdateService updateService = weakReference.get();
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS:
                    try {
                        JSONObject jsonObject = new JSONObject((String) msg.obj);
                        String last_ver = jsonObject.getString("last_ver");
                        //版本更新用的
                        if (!TextUtils.isEmpty(last_ver)) {
                            SPUtils.putString(Constant.LAST_VERSION, last_ver);
                            if (!VariousUtils.getVersion().equals(last_ver)) {
                                Variable.IS_NEW_VERSION = true;
                                Intent i = new Intent(Constant.ACTION_NEW_VERSION);
                                updateService.sendBroadcast(i);
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    try{
                        JSONObject jsonObject = new JSONObject((String) msg.obj);
                        String token = jsonObject.getString("token");
                        if (!TextUtils.isEmpty(token)) {
                            //接口版本控制用的
                            SPUtils.putString(Constant.TOKEN, token);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                default:
                    break;
            }

        }
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        myHandler1 = new MyHandler(this);
        MyLogUtils.e("wangic1");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        upLoadPhoneMessage();
        MyLogUtils.e("wangic2");
        return super.onStartCommand(intent, flags, startId);
    }


    //上传手机信息
    public void upLoadPhoneMessage() {
        String phoneType = android.os.Build.MODEL; // 手机型号
        String androidType = Build.VERSION.RELEASE; // 当前安卓系统版本
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("ver", VariousUtils.getVersion());
        hashMap.put("os", "android");
        hashMap.put("deviceinfo", phoneType + ":" + androidType);
        hashMap.put("devicetoken", "");
        hashMap.put("clientid", SPUtils.getSharedPreferences().getString(Constant.CLIENTID, ""));

        new InterNetController(this, Constant.DEVICEINFO, myHandler1, hashMap, 0, false);
    }
}
